package com.fan.leetcode;

import org.junit.jupiter.api.Test;

/**
 * 6. Z 字形变换
 *
 * @author ZhangFan
 * @since 2021/9/5 下午 6:07
 */
public class Question6 {
    @Test
    void test() {
        System.out.println("convert() = " + convert("A", 2));
    }

    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < numRows && i < s.length(); i++) {
            int index = i;
            str.append(s.charAt(index));
            int n = i * 2;
            int m = numRows * 2 - 2 - n;
            do {
                index += m;
                if (m != 0 && index < s.length()) {
                    str.append(s.charAt(index));
                }
                index += n;
                if (n != 0 && index < s.length()) {
                    str.append(s.charAt(index));
                }
            } while (index < s.length());
        }
        return String.valueOf(str);
    }
}
